package com.lanou.mynews.video;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.lanou.mynews.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class VideoAdapter extends BaseAdapter {
    private List<VideoEty.VideoListEntity> datas;
    private Context mContext;

    public VideoAdapter(Context context, List<VideoEty.VideoListEntity> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0)
            return datas.size();
        else
            return 0;
    }

    @Override
    public VideoEty.VideoListEntity getItem(int position) {
        if (datas != null && datas.size() > 0)
            return datas.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;


        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.video_content, null);
            holder = new ViewHolder();


            holder.textView_title = (TextView) convertView.findViewById(R.id.textView_title);
            holder.textView_description = (TextView) convertView.findViewById(R.id.textView_description);
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();


        }
        VideoEty.VideoListEntity videoety = getItem(position);
        if (videoety != null) {

//            holder.videoView.setVideoURI(Uri.parse(videoety.getMp4_url()));
//            holder.videoView.setMediaController(new MediaController(mContext));
//            holder.videoView.requestFocus();
            holder.textView_title.setText(videoety.getTitle());
            holder.textView_description.setText(videoety.getDescription());
            Picasso.with(mContext).load(videoety.getCover()).into(holder.imageView);

        }


        return convertView;
    }


    private class ViewHolder {
        public ImageView imageView;
        public TextView textView_title;
        public TextView textView_description;


    }


}
